/**
 * Created by Elsa on 2017/9/5.
 */

layui.define(['jquery', 'layer','paging', 'code','form','element','upload'], function(exports){
    var form = layui.form();
    var index;

    layui.code();
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer,
        upload = layui.upload,
        element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块

    var lognum = getQueryString("rgstnum");

    paging.init({
        url: contextPath + "/project/ops/log/" + lognum, //地址
        elem: '#con', //内容容器
        type: 'GET',
        params: { //发送到服务端的参数

        },
        tempElem: '#conTemp', //模块容器
        pageConfig: { //分页参数配置
            elem: '#page', //分页容器
            pageSize: 10 //分页大小
        },
        success: function() { //渲染成功的回调
        },
        fail: function(msg) { //获取数据失败的回调
        },
        complate: function() { //完成的回调
        },
    });

    $("body").on("click",".deletee",function(){
        var logtime = $(this).data('id');
        layer.alert('确定删除?',{
            skin: 'layui-layer-molv' //样式类名  自定义样式
            ,closeBtn: 1    // 是否显示关闭按钮
            ,anim: 1 //动画类型
            ,btn: ['确定','取消'] //按钮
            ,icon: 6    // icon
        },function(){
            $.ajax({
                type:"DELETE",
                url:contextPath+"/project/ops/log/delete?lognum="+lognum+"&logtime="+logtime,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "删除日志成功!"){
                        layer.msg('删除日志成功!<br>页面将在1秒后刷新...', {
                            time: 1000 //1s后自动关闭
                        });
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                    }
                },
                error:function (e) {
                }
            });
        });
    });

    $("body").on("click",".moress",function(){
        var logtime = $(this).data('id');
        tampdata = $(this).data('id');

        $.get('OperaDetailsLog.html', null, function(OperaDetailsLog) {
            layer.open({
                type: 1,
                title: '日志详情',
                content: OperaDetailsLog,
                area: ['auto', '600px'],
                maxmin: false,
                success:function (layero,index) {
                    layero.find("#closex").on("click",function () {
                        layer.closeAll();
                    });
                    layero.find("#close").on("click",function () {
                        layer.closeAll();
                    });

                    $.ajax({
                        type:"GET",
                        url:contextPath+"/project/ops/log/info",
                        data: {"lognum": lognum, "logtime": logtime},
                        contentType: "application/json; charset=utf-8",
                        dataType:"json",
                        success:function (data) {

                            if(data.logpicture1){
                                var logpicture1name = data.logpicture1.substring(data.logpicture1.lastIndexOf("/")+1,data.logpicture1.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture1name+'"><img src="'+contextPath+'/download?file='+data.logpicture1+'" class="layui-upload-img" id="img-'+logpicture1name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture1" href="'+contextPath+'/download?file='+data.logpicture1+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>');
                            }
                            if(data.logpicture2){
                                var logpicture2name = data.logpicture2.substring(data.logpicture2.lastIndexOf("/")+1,data.logpicture2.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture2name+'"><img src="'+contextPath+'/download?file='+data.logpicture2+'" class="layui-upload-img" id="img-'+logpicture2name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture2" href="'+contextPath+'/download?file='+data.logpicture2+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>');
                            }
                            if(data.logpicture3){
                                var logpicture3name = data.logpicture3.substring(data.logpicture3.lastIndexOf("/")+1,data.logpicture3.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture3name+'"><img src="'+contextPath+'/download?file='+data.logpicture3+'" class="layui-upload-img" id="img-'+logpicture3name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture3" href="'+contextPath+'/download?file='+data.logpicture3+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>');
                            }
                            if(data.logpicture4){
                                var logpicture4name = data.logpicture4.substring(data.logpicture4.lastIndexOf("/")+1,data.logpicture4.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture4name+'"><img src="'+contextPath+'/download?file='+data.logpicture4+'" class="layui-upload-img" id="img-'+logpicture4name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture4" href="'+contextPath+'/download?file='+data.logpicture4+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>');
                            }
                            if(data.logpicture5){
                                var logpicture5name = data.logpicture5.substring(data.logpicture5.lastIndexOf("/")+1,data.logpicture5.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture5name+'"><img src="'+contextPath+'/download?file='+data.logpicture5+'" class="layui-upload-img" id="img-'+logpicture5name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture5" href="'+contextPath+'/download?file='+data.logpicture5+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>');
                            }
                            if(data.logpicture6){
                                var logpicture6name = data.logpicture6.substring(data.logpicture6.lastIndexOf("/")+1,data.logpicture6.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture6name+'"><img src="'+contextPath+'/download?file='+data.logpicture6+'" class="layui-upload-img" id="img-'+logpicture6name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture6" href="'+contextPath+'/download?file='+data.logpicture6+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>');
                            }
                            if(data.logpicture7){
                                var logpicture7name = data.logpicture7.substring(data.logpicture7.lastIndexOf("/")+1,data.logpicture7.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture7name+'"><img src="'+contextPath+'/download?file='+data.logpicture7+'" class="layui-upload-img" id="img-'+logpicture7name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture7" href="'+contextPath+'/download?file='+data.logpicture7+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>');
                            }
                            if(data.logpicture8){
                                var logpicture8name = data.logpicture8.substring(data.logpicture8.lastIndexOf("/")+1,data.logpicture8.lastIndexOf("."));
                                $('#logpicturelist').append('<div style="display: inline-block" id="div-'+logpicture8name+'"><img src="'+contextPath+'/download?file='+data.logpicture8+'" class="layui-upload-img" id="img-'+logpicture8name+'"><p style="text-align: center;width: 110px"><a class="layui-btn layui-btn-normal" id="logpicture8" href="'+contextPath+'/download?file='+data.logpicture8+'" style="visibility: visible"><i class="fa fa-download"></i>下载</a></p></div>')
                            }
                            if(data.logvideo1){
                                var logvideo1name = data.logvideo1.substring(data.logvideo1.lastIndexOf("/")+1,data.logvideo1.lastIndexOf("."));
                                $('#logvideolist1').attr("href",contextPath+'/download?file='+data.logvideo1);
                                $('#logvideolist1name').html("&ensp;"+logvideo1name);
                                $('#logvideolist1').css("visibility",'visible');
                            }
                            if(data.logvideo2){
                                var logvideo2name = data.logvideo2.substring(data.logvideo2.lastIndexOf("/")+1,data.logvideo2.lastIndexOf("."));
                                $('#logvideolist2').attr("href",contextPath+'/download?file='+data.logvideo2);
                                $('#logvideolist2name').html("&ensp;"+logvideo2name);
                                $('#logvideolist2').css("visibility",'visible');
                            }

                            $('input[name="logname"]').attr("value",data.logname);
                            $('input[name="lognum"]').attr("value",data.lognum);
                            $('input[name="ycsname"]').attr("value",data.ycsname);
                            $('input[name="insptype"]').attr("value",data.insptype);
                            $('input[name="address"]').attr("value",data.address);
                            $('input[name="logtime"]').attr("value",data.logtime);
                            $('textarea[name="logcontent"]').val(data.logcontent);
                        },
                        error:function (e) {
                        }
                    });

                },
                yes: function(index) {
                    console.log(index);
                },
                full: function(elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function() {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
            form.render();
        });
    });

    $('body').on('click', '#close', function(){
        layer.close(index);
        return false;
    });

    $('body').on('click', '#closex', function(){
        layer.close(index);
        return false;
    });

    $('#add').on('click', function() {
        $.get('OperaNewLogList.html', null, function(OperaNewLogList) {
            layer.open({
                type: 1,
                title: '新建日志',
                content: OperaNewLogList,
                area: ['auto', '600px'],
                maxmin: false,
                success:function (layero,index) {
                    layero.find("#closex").on("click",function () {
                        layer.closeAll();
                    });

                    var OperaNewLogList = new Object();
                    OperaNewLogList.logpicture1 = "";
                    OperaNewLogList.logpicture2 = "";
                    OperaNewLogList.logpicture3 = "";
                    OperaNewLogList.logpicture4 = "";
                    OperaNewLogList.logpicture5 = "";
                    OperaNewLogList.logpicture6 = "";
                    OperaNewLogList.logpicture7 = "";
                    OperaNewLogList.logpicture8 = "";
                    OperaNewLogList.logvideo1 = "";
                    OperaNewLogList.logvideo2 = "";

                    var filenum = 'operalog';

                    //多图片上传
                    upload.render({
                        elem: '#test2'
                        ,size: 51200
                        ,url: contextPath + '/uploadAll?username=' + lognum + '&filenum='+ filenum
                        ,multiple: true
                        ,before: function(obj){
                            //预读本地文件示例，不支持ie8
                            obj.preview(function(index, file, result){
                                var inx = index.split('-');
                                var num = parseInt(inx[1])+1;
                                var xx = 1;
                                if(0<num&&num<9){
                                    $('#demo2').append('<div style="display: inline-block" id="div-'+index+'"><img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" id="img-'+index+'"><p style="text-align: center"><button class="layui-btn layui-btn-mini layui-btn-danger" id="img-delete-'+index+'">删除</button></p></div>')
                                }
                                if(num>=9&&xx==1){
                                    layer.msg("最多上传8张图片!", {
                                        time:false,
                                        btn:"确定",
                                        btnAlign: 'c'
                                    });
                                    xx=xx+1;
                                }
                            });
                        }
                        ,done: function(res, index){
                            //上传完毕
                            layer.msg('上传成功...',{time: 1000});

                            if(res.code == 0){
                                var inx = index.split('-');
                                var num = parseInt(inx[1])+1;

                                if(num == 1){
                                    OperaNewLogList.logpicture1 = res.data.url;
                                } else if(num == 2){
                                    OperaNewLogList.logpicture2 = res.data.url;
                                } else if(num == 3){
                                    OperaNewLogList.logpicture3 = res.data.url;
                                } else if(num == 4){
                                    OperaNewLogList.logpicture4 = res.data.url;
                                } else if(num == 5){
                                    OperaNewLogList.logpicture5 = res.data.url;
                                } else if(num == 6){
                                    OperaNewLogList.logpicture6 = res.data.url;
                                } else if(num == 7){
                                    OperaNewLogList.logpicture7 = res.data.url;
                                } else if(num == 8){
                                    OperaNewLogList.logpicture8 = res.data.url;
                                }

                                console.log(res);
                                $('#img-delete-'+index).on('click',function () {
                                    console.log(res.data.url);
                                    $.ajax({
                                        type:"GET",
                                        url:contextPath+"/deletefile?file="+res.data.url,
                                        contentType: "application/json; charset=utf-8",
                                        dataType:"json",
                                        success:function (data) {
                                            console.log(data);
                                            if(data == "删除成功!"){
                                                $('#div-'+index).remove();

                                                if(num == 1){
                                                    OperaNewLogList.logpicture1 = "";
                                                } else if(num == 2){
                                                    OperaNewLogList.logpicture2 = "";
                                                } else if(num == 3){
                                                    OperaNewLogList.logpicture3 = "";
                                                } else if(num == 4){
                                                    OperaNewLogList.logpicture4 = "";
                                                } else if(num == 5){
                                                    OperaNewLogList.logpicture5 = "";
                                                } else if(num == 6){
                                                    OperaNewLogList.logpicture6 = "";
                                                } else if(num == 7){
                                                    OperaNewLogList.logpicture7 = ""
                                                } else if(num == 8){
                                                    OperaNewLogList.logpicture8 = "";
                                                }

                                                layer.msg('上传文件已删除...',{time: 1000});
                                                return false;
                                            } else {
                                                layer.msg(data, {
                                                    time:false,
                                                    btn:"确定",
                                                    btnAlign: 'c'
                                                });
                                            }
                                        },
                                        error:function (e) {
                                        }
                                    });
                                });
                            }

                        }
                    });

                    //上传影像
                    var demoListView = $('#demoList')
                        ,uploadListIns = upload.render({
                        elem: '#testList'
                        ,url: contextPath + '/uploadAll?username=' + lognum + '&filenum=' + filenum
                        ,accept: 'file'
                        ,size: 1048576
                        ,exts: 'mpeg|mpg|avi|mov|wmv|navi|3gp|mkv|flv|f4v|rmvb|mp4'
                        ,multiple: true
                        ,auto: false
                        ,bindAction: '#testListAction'
                        ,choose: function(obj){
                            var files = obj.pushFile(); //将每次选择的文件追加到文件队列
                            //读取本地文件
                            obj.preview(function(index, file, result){
                                var inx = index.split('-');
                                var num = parseInt(inx[1])+1;
                                var xx = 1;
                                if(1<=num&&num<=2){
                                    var tr = $(['<tr id="upload-'+ index +'">'
                                        ,'<td>'+ file.name +'</td>'
                                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                                        ,'<td>等待上传</td>'
                                        ,'<td>'
                                        ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                                        ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">取消</button>'
                                        ,'</td>'
                                        ,'</tr>'].join(''));
                                }
                                if(num>=3&&xx==1){
                                    layer.msg("最多上传2段视频!", {
                                        time:false,
                                        btn:"确定",
                                        btnAlign: 'c'
                                    });
                                    xx=xx+1;
                                }
                            
                                //单个重传
                                tr.find('.demo-reload').on('click', function(){
                                    obj.upload(index, file);
                                });

                                //删除
                                tr.find('.demo-delete').on('click', function(){
                                    delete files[index]; //删除对应的文件
                                    tr.remove();
                                });

                                demoListView.append(tr);
                            });
                        }
                        ,done: function(res, index, upload){
                            if(res.code == 0){ //上传成功
                                var inx = index.split('-');
                                var num = parseInt(inx[1])+1;

                                if(num == 1){
                                    OperaNewLogList.logvideo1 = res.data.url;
                                } else if(num == 2){
                                    OperaNewLogList.logvideo2 = res.data.url;
                                }

                                var tr = demoListView.find('tr#upload-'+ index)
                                    ,tds = tr.children();
                                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                                tds.eq(3).html('<button class="layui-btn layui-btn-mini layui-btn-danger file-delete">删除</button>'); //添加取消上传按钮

                                //删除已上传的文件
                                tr.find('.file-delete').on('click',function () {
                                    console.log(res.data.url);
                                    $.ajax({
                                        type:"GET",
                                        url:contextPath+"/deletefile?file="+res.data.url,
                                        contentType: "application/json; charset=utf-8",
                                        dataType:"json",
                                        success:function (data) {
                                            console.log(data);
                                            if(data == "删除成功!"){
                                                tr.remove();

                                                if(num == 1){
                                                    OperaNewLogList.logvideo1 = "";
                                                } else if(num == 2){
                                                    OperaNewLogList.logvideo2 = "";
                                                }

                                                layer.msg('上传文件已删除...',{time: 1000});
                                                return false;
                                            } else {
                                                layer.msg(data, {
                                                    time:false,
                                                    btn:"确定",
                                                    btnAlign: 'c'
                                                });
                                            }
                                        },
                                        error:function (e) {
                                        }
                                    });
                                });

                                return;
                            }
                            this.error(index, upload);
                        }
                        ,error: function(index, upload){
                            var tr = demoListView.find('tr#upload-'+ index)
                                ,tds = tr.children();
                            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                        }
                    });

                    $.ajax({
                        type: "GET",
                        url: contextPath + "/personal/info",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            $('input[name="ycsname"]').attr("value",data.name);
                        },
                        error: function (e) {

                        }
                    });

                    $('input[name="insptype"]').attr("value","营运检验");
                    $('input[name="lognum"]').attr("value",lognum);
                    $('input[name="logtime"]').attr("value",getNowFormatDate());

                    //监听提交
                    form.on('submit(caseglyx)', function(data) {
                        $(this).attr('disabled','disabled');

                        OperaNewLogList.lognum = lognum;
                        OperaNewLogList.logname = $('input[name="logname"]').val();
                        OperaNewLogList.ycsname = $('input[name="ycsname"]').val();
                        OperaNewLogList.insptype = $('input[name="insptype"]').val();
                        OperaNewLogList.address = $('input[name="address"]').val();
                        OperaNewLogList.logcontent = $('textarea[name="logcontent"]').val();


                        console.log(JSON.stringify(OperaNewLogList));

                        $.ajax({
                            type:"POST",
                            url:contextPath+"/project/ops/log/new",
                            contentType: "application/json; charset=utf-8",
                            data:JSON.stringify(OperaNewLogList),
                            dataType:"json",
                            success:function (data) {
                                console.log(data.message);
                                if(data.message == "日志新建成功!"){
                                    layer.msg('日志新建成功!<br>页面将在1秒后刷新...');
                                    setTimeout(function(){window.location.reload();},1000);
                                } else {
                                    $('#suresubmitx').attr('disabled',false);
                                    layer.msg(data.message, {
                                        time:false,
                                        btn:"确定",
                                        btnAlign: 'c'
                                    });
                                }
                            },
                            error:function (e) {
                            }
                        });
                        return false;
                    });
                },
                yes: function(index) {
                    console.log(index);
                },
                full: function(elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function() {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                }
            });
            form.render();
        });
    });


    exports('OperaLogList', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});